package com.youku.plugins.toolboxV4.view
{
   import flash.display.Sprite;
   import com.youku.baseUI.YoukuTextField;
   import fl.controls.RadioButtonGroup;
   import flash.utils.Dictionary;
   import com.youku.plugins.toolboxV4.ToolboxFont;
   import fl.controls.ButtonLabelPlacement;
   import flash.events.MouseEvent;
   import flash.events.Event;
   import com.youku.utils.AssetUtil;
   import flash.text.TextFieldAutoSize;
   import com.youku.utils.ReportUtil;
   import com.youku.plugins.toolboxV4.ToolboxEvent;
   
   public class QualityPanel extends Sprite
   {
      
      public function QualityPanel() {
         super();
         this.init();
      }
      
      private var _title:YoukuTextField;
      
      private var _tbGroup:RadioButtonGroup;
      
      private var _tbDefer:Array;
      
      private var _tbSelectors:Dictionary;
      
      private var _hasHigh:Boolean;
      
      private var _hasMid:Boolean;
      
      private var _has1080P:Boolean;
      
      private function init() : void {
         var _loc2_:ToolBoxRadioButton = null;
         this._hasHigh = false;
         this._hasMid = false;
         this._has1080P = false;
         this._tbDefer = new Array();
         this._tbSelectors = new Dictionary();
         this._tbGroup = new RadioButtonGroup("quality");
         this._tbDefer.push(
            {
               "type":"hd3",
               "name":"1080P"
            });
         this._tbDefer.push(
            {
               "type":"hd2",
               "name":"超清"
            });
         this._tbDefer.push(
            {
               "type":"mp4",
               "name":"高清"
            });
         this._tbDefer.push(
            {
               "type":"flv",
               "name":"标清"
            });
         var _loc1_:Number = 0;
         while(_loc1_ < this._tbDefer.length)
         {
            _loc2_ = new ToolBoxRadioButton();
            _loc2_.group = this._tbGroup;
            _loc2_.costomTextFormat = ToolboxFont.FONT_SELECTOR;
            _loc2_.labelPlacement = ButtonLabelPlacement.RIGHT;
            _loc2_.text = this._tbDefer[_loc1_].name;
            _loc2_.label = "";
            _loc2_.fontHoffset = -2;
            _loc2_.value = this._tbDefer[_loc1_].type;
            this._tbSelectors[this._tbDefer[_loc1_].type] = _loc2_;
            _loc2_.addEventListener(MouseEvent.CLICK,this.onSelectorClick);
            _loc1_++;
         }
         this._tbGroup.addEventListener(Event.CHANGE,this.onQualityChange);
         this._title = AssetUtil.getYoukuTextField(false);
         this._title.defaultTextFormat = ToolboxFont.FONT_COMMON;
         this._title.autoSize = TextFieldAutoSize.LEFT;
         this._title.fontHoffset = -2;
         this._title.text = "当前画质:";
         this.addChild(this._title);
         this.resetOptions();
      }
      
      private function onSelectorClick(param1:MouseEvent) : void {
         var _loc2_:ToolBoxRadioButton = param1.target as ToolBoxRadioButton;
         switch(_loc2_.value as String)
         {
            case "hd3":
               ReportUtil.sendUserActionLog(ReportUtil.SETTINGS_PANEL_PRESENT_QUALITY_1080P_DEFINITION);
               break;
            case "hd2":
               ReportUtil.sendUserActionLog(ReportUtil.SETTINGS_PANEL_PRESENT_QUALITY_SUPER_DEFINITION);
               break;
            case "mp4":
               ReportUtil.sendUserActionLog(ReportUtil.SETTINGS_PANEL_PRESENT_QUALITY_HIGH_DEFINITION);
               break;
            case "flv":
               ReportUtil.sendUserActionLog(ReportUtil.SETTINGS_PANEL_PRESENT_QUALITY_NORMAL_HIGH_DEFINITION);
               break;
         }
      }
      
      private function onQualityChange(param1:Event) : void {
         var _loc2_:ToolBoxRadioButton = null;
         for each(_loc2_ in this._tbSelectors)
         {
            if(_loc2_ == this._tbGroup.selection)
            {
               _loc2_.selected = true;
            }
            else
            {
               _loc2_.selected = false;
            }
         }
         if(this._tbGroup.selection == this._tbSelectors["hd3"])
         {
            dispatchEvent(new ToolboxEvent(ToolboxEvent.QUALITY_1080P_CLICK));
         }
      }
      
      public function show() : void {
      }
      
      private function updateLayout() : void {
         this._title.x = 11;
         this._title.y = 4;
         this._tbSelectors["hd3"].setWH(56,18);
         this._tbSelectors["hd3"].x = this._title.x + this._title.width + 6;
         this._tbSelectors["hd3"].y = 0;
         this._tbSelectors["hd2"].setWH(44,18);
         this._tbSelectors["hd2"].x = this._tbSelectors["hd3"].x + this._tbSelectors["hd3"].width;
         this._tbSelectors["hd2"].y = 0;
         this._tbSelectors["mp4"].setWH(44,18);
         this._tbSelectors["mp4"].x = this._tbSelectors["hd2"].x + this._tbSelectors["hd2"].width;
         this._tbSelectors["mp4"].y = 0;
         this._tbSelectors["flv"].setWH(44,18);
         this._tbSelectors["flv"].x = this._tbSelectors["mp4"].x + this._tbSelectors["mp4"].width;
         this._tbSelectors["flv"].y = 0;
      }
      
      public function selectQuality(param1:String) : void {
         switch(param1)
         {
            case "flv":
            case "flvhd":
            case "low":
               this._tbSelectors["flv"].selected = true;
               break;
            case "mp4":
            case "mid":
               this._tbSelectors["mp4"].selected = true;
               break;
            case "high":
            case "hd2":
               this._tbSelectors["hd2"].selected = true;
               break;
            case "1080P":
            case "hd3":
               this._tbSelectors["hd3"].selected = true;
               break;
         }
      }
      
      private function resetOptions() : void {
         var _loc1_:ToolBoxRadioButton = null;
         for each(_loc1_ in this._tbSelectors)
         {
            if(contains(_loc1_))
            {
               removeChild(_loc1_);
            }
         }
         addChild(this._tbSelectors["hd3"]);
         addChild(this._tbSelectors["hd2"]);
         addChild(this._tbSelectors["mp4"]);
         addChild(this._tbSelectors["flv"]);
         this.updateLayout();
      }
      
      public function setQualities(param1:Array) : void {
         var _loc2_:String = null;
         this._hasHigh = false;
         this._hasMid = false;
         this._has1080P = false;
         for each(_loc2_ in param1)
         {
            if(_loc2_ == "hd3")
            {
               this._has1080P = true;
            }
            else if(_loc2_ == "hd2")
            {
               this._hasHigh = true;
            }
            else if(_loc2_ == "mp4")
            {
               this._hasMid = true;
            }
            
            
         }
         this._tbSelectors["hd3"].enabled = false;
         this._tbSelectors["hd2"].enabled = false;
         this._tbSelectors["mp4"].enabled = false;
         this._tbSelectors["flv"].enabled = true;
         if(this._hasMid)
         {
            this._tbSelectors["mp4"].enabled = true;
         }
         if(this._hasHigh)
         {
            this._tbSelectors["hd2"].enabled = true;
         }
         if(this._has1080P)
         {
            this._tbSelectors["hd3"].enabled = true;
         }
      }
      
      public function getQuality() : String {
         var _loc1_:* = "flv";
         if(this._tbGroup.selection)
         {
            _loc1_ = this._tbGroup.selection.value as String;
         }
         return _loc1_;
      }
   }
}
